//
//  ViewController.m
//  Web Browser
//
//  Created by iD Student on 7/11/13.
//  Copyright (c) 2013 Apple Inc. All rights reserved.
//

#import "ViewController.h"

@end
@interface ViewController ()

@end

@implementation ViewController

{
    NSMutableArray *_arrayHistory;
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    

}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    
    [_addressBar setText:[[webView.request URL] absoluteString]];
    [activity startAnimating];
    [self setBackForwardButtonState];
    
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    [_addressBar setText:[[webView.request URL] absoluteString]];
    [activity stopAnimating];
    [self setBackForwardButtonState];
    if(_arrayHistory.lastObject == [_addressBar text])
    {
        
    
    } else {
        [_arrayHistory addObject:[[webView.request URL] absoluteString]];
        NSLog(@"%@" , _arrayHistory.description);
    }
    
}

- (void)viewDidLoad
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice" message:@"History Controller Has Not Yet Been Implemtnted." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil, nil, nil];
    [alert show];
    [alert release];
    _arrayHistory = [[NSMutableArray alloc] init];
    
    [self.webView addSubview: activity];
    [self.webView setDelegate:self];
    [super viewDidLoad];
    NSString *address = @"";
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_searchField setDelegate:self];
    [_addressBar setDelegate:self];
    [_webView loadRequest:request];
    [_addressBar setText:address];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
     {
         NSURL *url = [NSURL URLWithString:[@"http://" stringByAppendingString:_addressBar.text ]];
         NSURLRequest *request = [NSURLRequest requestWithURL:url];
         [_webView loadRequest:request];
         [_addressBar resignFirstResponder];
         return YES;
     }
     


- (void) searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    
    NSString *address = [@"https://www.google.com/search?q=" stringByAppendingString:_searchField.text];
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
    [_addressBar setText:address];
    [_searchField resignFirstResponder];
    
    
}
    
-(IBAction)search:(id)sender {
    NSURL *url = [NSURL URLWithString:_addressBar.text];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    [_addressBar resignFirstResponder];
    
}
    
    - (void) setBackForwardButtonState{

        
        [self.buttonBack setEnabled:[_webView canGoBack]];
        [_addressBar setText:[[_webView.request URL] absoluteString]];
        
        [self.buttonForward setEnabled:[_webView canGoForward]];
        [_addressBar setText:[[_webView.request URL] absoluteString]];
        
    }

-(IBAction)homeButton:(id)sender {
    NSString *address = @"http://www.google.com";
    NSURL *url = [NSURL URLWithString:address];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_addressBar setText:address];
    [_webView loadRequest:request];
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void) goAddress:(id)sender
{
    NSURL *url = [NSURL URLWithString:[_addressBar text]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
    [_addressBar resignFirstResponder];
}
- (IBAction)searchInGoogle:(id)sender {
}

- (void)awakeFromNib {
    [self refreshclicks:self];
}

- (IBAction)refreshClicks:(id)sender
{
    [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[@"" stringByAppendingString:[_addressBar text]]]]];
}


- (void) goBack: (id)sender{
    [_webView goBack];
}
- (void) goForward: (id)sender{
    [_webView goForward];
}


- (void)dealloc {
    [_searchField release];
    [_homeButton release];
    [_buttonBack release];
    [_buttonForward release];
    [_buttonForward release];
    [super dealloc];
}
@end


